home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
5 Star Games: DOS Edition 2
/
5 Star Games - DOS Edition (1995)(Ready to Run).iso
/
dbc
/
db_file.c
< prev
next >
Wrap
C/C++ Source or Header
|
1992-03-04
|
6KB
|
294 lines
/****************************************************************************/
/* DATABOSS MODULE: DB_FILE.C */
/****************************************************************************/
#include "db_lsc.h"
#include <ctype.h>
#ifdef __TURBOC__
#include <dir.h>
#else
#include <direct.h>
#endif
#include <dos.h>
#include <errno.h>
#include <io.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "db_types.h"
#include "db_str.h"
#include "db_file.h"
/*************************** INTERNAL VARIABLES ***************************/
static string dospath = "PATH=";
static uchar drivespec[4] = "?:\\";
/***************************** IMPLEMENTATION *****************************/
strptr getenvstr(strptr sout, string envar)
{
strptr p;
return (strcpy(sout,((p = getenv(envar)) == NULL) ? "\0" : p));
}
strptr fsearchdir(pathstr sout, pathstr path)
{
pathstr savepath;
int plen;
if (path[0] != 0) {
getcurrentdir(savepath,0);
if (changedir(path)) {
getcurrentdir(sout,0);
plen = strlen(sout);
if (sout[plen-1] != '\\') {
sout[plen++] = '\\';
sout[plen] = '\0';
}
}
else
sout[0] = '\0';
changedir(savepath);
}
else
sout[0] = '\0';
return (sout);
}
strptr fsearchpath(pathstr sout, pathstr path)
{
pathstr tpath;
namestr name;
extstr ext;
findrec frec;
int plen;
if (path[0] != '\0') {
plen = strlen(path);
if ((plen >= 2) && (path[1] == ':') && (path[2] != '\\')) {
strcopy(tpath,path,0,2);
strcat(fsearchdir(tpath,tpath),&path[2]);
plen = strlen(tpath);
}
else
strcpy(tpath,path);
if (tpath[plen-1] == '\\') {
if ((plen == 3) && (tpath[1] == ':'))
strcpy(sout,tpath);
else
fsearchdir(sout,tpath);
}
else {
if (db_findfirst(tpath,&frec,A_DIREC)) {
if ((frec.attrib & A_DIREC) != 0)
fsearchdir(sout,tpath);
else {
fsplit(tpath,sout,name,ext);
strconcat(sout,fsearchdir(tpath,sout),name,ext,NULL);
}
}
else
sout[0] = '\0';
}
}
else
sout[0] = '\0';
return (strupr(sout));
}
strptr fsearch(pathstr fnout, pathstr fnin, string path)
{
string userpath;
namestr name;
extstr ext;
#ifdef __TURBOC__
strptr p;
#else
char pathbuf[256];
#endif
if (*fnin != '\0') {
fsplit(fnin,userpath,name,ext);
if (*userpath != '\0')
fsearchpath(fnout,fnin);
else {
getenvstr(&dospath[5],"PATH");
strcat(stpcpy(userpath,"PATH="),path);
#ifdef __TURBOC__
if ((p = (putenv(userpath) == 0) ? searchpath(fnin) : NULL) != NULL)
strcpy(fnout,p);
else
*fnout = '\0';
#else
if (putenv(userpath) == 0) {
_searchenv(fnin,"PATH",pathbuf);
strcpy(fnout,pathbuf);
}
else
*fnout = '\0';
#endif
putenv(dospath);
}
}
else
*fnout = '\0';
return(fnout);
}
void fsplit(pathstr path, dirstr dir, namestr name, extstr ext)
{
drivestr drive;
#ifdef __TURBOC__
fnsplit(path,drive,dir,name,ext);
#else
_splitpath(path,drive,dir,name,ext);
#endif
strinsert(drive,dir,0);
}
strptr fullspec(pathstr sout, pathstr path)
{
union REGS regs;
pathstr cdir;
strcpy(sout,path);
if ((strlen(path) < 2) || (path[1] != ':')) {
regs.h.ah = 0x19;
intdos(®s,®s);
strinsert("?:",sout,0);
sout[0] = (char)('A'+regs.h.al);
}
if (sout[2] != '\\') {
getcurrentdir(cdir,(byte)(toupper(sout[0])-'A'+1));
if (cdir[strlen(cdir)-1] != '\\')
strchcat(cdir,'\\');
strinsert(&cdir[2],sout,2);
}
return (sout);
}
strptr fexpand(pathstr sout, pathstr path)
{
pathstr tpath;
strptr ip,op;
int dots;
if (path[0] != '\0') {
fullspec(tpath,path);
ip = tpath;
op = sout;
do {
while ((*ip != '\\') && (*ip != '\0')) *op++ = *ip++;
if (*ip == '\\') {
if (*(op-1) != '\\') *op++ = '\\';
ip++;
dots = 0;
while (*ip == '.') {
dots++;
ip++;
}
if (dots-- > 0) {
while ((*ip != '\\') && (*ip != '\0')) ip++;
while (dots > 0) {
if (*(op-2) != ':') {
do op--; while (*(op-1) != '\\');
}
dots--;
}
}
}
} while (*ip != '\0');
*op = '\0';
}
else
getcurrentdir(sout,0);
strupr(sout);
return (sout);
}
bool getcurrentdir(pathstr sout, byte drive)
{
union REGS regs;
struct SREGS sregs;
if (drive == 0) {
regs.h.ah = 0x19;
intdos(®s,®s);
drivespec[0] = (uchar) (regs.h.al+'A');
}
else
drivespec[0] = (uchar) (drive+'A'-1);
strcpy(sout,drivespec);
regs.h.ah = 0x47;
regs.h.dl = drive;
regs.x.si = FP_OFF(sout)+3;
sregs.ds = FP_SEG(sout);
intdosx(®s,®s,&sregs);
if (regs.x.cflag) sout[3] = '\0';
return ((bool) (regs.x.cflag == 0));
}
bool changedrive(uchar drive)
{
union REGS regs;
byte dosdrive;
dosdrive = toupper(drive) - 'A';
regs.h.ah = 0x0E;
regs.h.dl = dosdrive;
intdos(®s,®s);
regs.h.ah = 0x19;
intdos(®s,®s);
return ((bool) (regs.h.al == dosdrive));
}
bool changedir(pathstr path)
{
bool changeok;
pathstr tpath;
int plen;
if (path[0] != '\0') {
strcpy(tpath,path);
plen = strlen(tpath);
if ((plen > 1) && (tpath[plen-1] == '\\') &&
!((plen == 3) && (tpath[1] == ':')))
tpath[--plen] = '\0';
changeok = (tpath[1] == ':') ? changedrive(tpath[0]) : True;
if (changeok && !((plen == 2) && (tpath[1] == ':')))
changeok = (bool) (chdir(tpath) == 0);
}
else
changeok = False;
return (changeok);
}
bool db_eof(FILE *f)
{
return ((bool) (ftell(f) >= filelength(fileno(f))));
}
bool db_findfirst(pathstr path, findrec *frec, int attr)
{
#ifdef __TURBOC__
return ((bool) (findfirst(path,(struct ffblk *) frec,attr) == 0));
#else
return ((bool) (_dos_findfirst(path,attr,(struct find_t *) frec) == 0));
#endif
}
bool db_findnext(findrec *frec)
{
#ifdef __TURBOC__
return ((bool) (findnext((struct ffblk *) frec) == 0));
#else
return ((bool) (_dos_findnext((struct find_t *) frec) == 0));
#endif
}
/***************************** END OF DB_FILE.C ***************************/